<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="closeDialog"
    :close-on-click-modal="false"
    width="50%"
    top="5vh"
  >
    <el-form
      ref="createOrEditForm"
      v-loading="loading"
      label-position="right"
      label-width="100px"
      :model="form"
      :rules="formRules"
    >
      <el-form-item :label="$t('BookStore2[\'Display:DriveTime\']')" prop="driveTime">
        <el-date-picker v-model="form.driveTime" type="datetime" :placeholder="$t('BookStore2[\'Display:DriveTime\']')" style="width: 100%;" />
      </el-form-item>
      <el-form-item :label="$t('BookStore2[\'Display:Driver\']')" prop="driver">
        <el-select v-model="form.driver" filterable :placeholder="$t('BookStore2[\'Display:Driver\']')" style="width: 100%;">
          <el-option v-for="item in userList" :key="item.value" :label="item.name + ' ' + item.surname + ' ( ' + item.userName + ' ) '" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('BookStore2[\'Display:Remark\']')" prop="remark">
        <el-input v-model="form.remark" type="textarea" />
      </el-form-item>
      <!-- 隐藏表单 -->
      <el-form-item prop="id" hidden>
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item prop="vehicleId" hidden>
        <el-input v-model="form.vehicleId" />
      </el-form-item>
      <el-form-item prop="concurrencyStamp" hidden>
        <el-input v-model="form.concurrencyStamp" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">{{ $t('BookStore2[\'Cancel\']') }}</el-button>
      <el-button type="primary" @click="submitForm">
        <i class="el-icon-check" />
        {{ $t('BookStore2[\'Save\']') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getById,
  create,
  edit
} from '@/api/vehicle/vehicleDriveRecord'
import { getList as getUserList } from '@/api/administration/user/user'
export default {
  props: {
    context: {
      type: Object,
      required: true
    },
    visible: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  data() {
    return {
      title: '',
      actionMode: '',
      loading: false,
      userList: [],
      form: {
        id: '',
        vehicleId: '',
        concurrencyStamp: '',
        driver: '',
        driveTime: '',
        remark: ''
      },
      formRules: {
        driver: [{ required: true, message: this.$t('BookStore2[\'ThisFieldIsRequired\']'), trigger: 'blur' }],
        driveTime: [{ required: true, message: this.$t('BookStore2[\'ThisFieldIsRequired\']'), trigger: 'blur' }],
      }
    }
  },
  watch: {
    context(newVal, oldVal) {
      this.loading = true
      this.title = newVal.title
      this.actionMode = newVal.actionMode
      this.$nextTick(function() {
        if (this.actionMode === 'edit') {
          /* 编辑模式 */
          if (newVal.id) {
            getById(newVal.id).then(resp => {
              if (resp) {
                this.form.id = resp.id
                this.form.concurrencyStamp = resp.concurrencyStamp
                this.form.startDate = resp.startDate
                this.form.endDate = resp.endDate
                this.form.location = resp.location
                this.form.followUpPerson = resp.followUpPerson
                this.form.cost = resp.cost
                this.form.failureCause = resp.failureCause
                this.form.technician = resp.technician
                this.form.detail = resp.detail
                this.form.remark = resp.remark
              }
              this.loading = false
            })
          }
        } else {
          /* 创建模式 */
          this.form.vehicleId = newVal.vehicleId
          this.loading = false
        }
      })
    }
  },
  async created() {
    this.userList = (await getUserList()).items
  },
  methods: {
    submitForm() {
      this.$refs.createOrEditForm.validate(async(isValid, invalidFields) => {
        if (isValid) {
          if (this.actionMode === 'edit') {
            await edit(this.form.id, this.form)
          } else {
            await create(this.form)
          }
          this.$message({ message: this.$t('BookStore2[\'Ok\']'), type: 'success' })
          this.closeDialog()
          this.$emit('saveSucceed')
        }
      })
    },
    closeDialog() {
      this.resetDialog() // 重置表单
      this.$emit('update:visible', false)
    },
    resetDialog() {
      this.$refs.createOrEditForm.resetFields()
      // console.log(this.form)
    }
  }
}
</script>

<style scoped>
</style>
